<template>
	<ls-page :hidden="true" :hideTopbar="true" backgroundImage="https://oss.jtmckj.com/wmp/kbq/i_back1.png" :default-bg="false">
		<!-- 白名单 - 直接进入 -->
	</ls-page>
</template>

<script setup>
	import { onLoad, onShow } from '@dcloudio/uni-app';
	import { HFiveLogin, HFiveRegister, GetBaseConfigInfoH5,HFiveAppObjProcess,HFiveMemberAppObjProcess } from '@/api/public.js'
	
	import { useTableStore } from '@/store/table.js';
	import { useUserStore } from '@/store/user.js';
	import { useShopStore } from '@/store/shop.js';
	import { useCardStore } from '@/store/card.js';
	const storeTable = useTableStore();
	const storeUser = useUserStore();
	const storeShop = useShopStore();
	const storeCard = useCardStore(); 
		
		
    // 微信code
	onLoad((options) => {
		console.log('onLoad...：',options.code)
		// getWxUserInfoCode()
		if(options.code){
			getWxInFo(options.code)
		} 
	});
	
	// 
	onShow(() => {
		//  
	});  

	// 后端获取
	const getWxInFo = async (code) => { 
		uni.showLoading({
			title: '正在加载'
		});
		let modal = {  "code": code }
		const data = await HFiveLogin(modal)
		console.log('HFiveLogin-userinfo:', data) 
		try{
			if(!data.userInfo.token){
				uni.showToast({ title: 'enter白名单的token不存在~', icon: 'none' })   
				return
			}
		}catch(e){
			//TODO handle the exception
		}
        uni.setStorageSync("token", data.userInfo.token)  
		// 已注册
		if (data.loginstatus === 1 && data.userInfo) {    
			const shopInfo  = await GetBaseConfigInfoH5() 
			let cardsModal = { "functionName":"MemberInfo_H5", "contentData": ""}
			const cardsInfo  = await HFiveMemberAppObjProcess(cardsModal) 
			// 必然cardsInfo是有数据回来
			const oneCardInfo = cardsInfo[0] 
			storeUser.SetUserInfo(data.userInfo)
			storeShop.SetShopInfo(shopInfo)
			storeCard.SetCardInfo(oneCardInfo) 
			
			// 桌台信息
			let modalTable = { "functionName": "GetRoomInfo_H5", "contentData": { "roomCode": '0017001' } }
			const tableInfo = await HFiveMemberAppObjProcess(modalTable)   
			storeTable.SetTableInfo(tableInfo)
			
			// uni.setStorageSync('userInfo',data.userInfo) 
			// uni.setStorageSync('shopInfo',shopInfo)
			// uni.setStorageSync('cardInfo', oneCardInfo) 
			uni.hideLoading();
			uni.showToast({ title: '欢迎您的到来~', icon: 'none' })   
			setTimeout(function() {
				uni.switchTab({
					url: '/pages/home/index'
				})
			}, 1200);
		} else {
			// 未注册
			uni.showToast({ title: '当前用户暂未注册或者用户信息不正确', icon: 'none' }) 
			// setTimeout(function() {
			// 	uni.switchTab({
			// 		url: '/pages/home/index'
			// 	})
			// }, 2000); 
		}  
	}

 
</script>

<style scoped lang="less">

</style>